package com.yisu.expressway.manager.website.controller.goodsArchives;

import com.yisu.expressway.ewf.common.web.EasyUIGrid;
import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.SpecificationGoods;
import com.yisu.expressway.manager.core.service.IGoodsArchivesService;
import com.yisu.expressway.manager.core.service.ISpecificationGoodsService;
import com.yisu.expressway.manager.domain.request.GoodsArchivesRequest;
import com.yisu.expressway.manager.domain.request.SalesGoodsRequest;
import com.yisu.expressway.manager.domain.vo.SalesGoodsVo;
import com.yisu.expressway.manager.website.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import static com.yisu.expressway.ewf.common.enums.ResponseCode.FAILURE;

/**
 *
 * User: 赵杰
 * Date: 2017/5/9
 * Time: 11:37
 * Desc: 售卖商品管理.
 */
@Controller
@RequestMapping(value = "/salesGoods")
public class SalesGoodsController extends BaseController {
    @Resource
    private IGoodsArchivesService iGoodsArchivesService;
    @Resource
    private ISpecificationGoodsService iSpecificationGoodsService;

    @RequestMapping(value = "/index.htm")
    public String listPage(Model model, SalesGoodsRequest salesGoodsRequest) {
        try {
            if (!StringUtils.isEmpty(salesGoodsRequest.getKeyword())) {
                salesGoodsRequest.setKeyword(java.net.URLDecoder.decode(salesGoodsRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<SalesGoodsVo> salesGoodsList = iGoodsArchivesService.getSalesGoodsListByPage(salesGoodsRequest);
            model.addAttribute("SalesGoodsVos", salesGoodsList.getRows());
            model.addAttribute("currentPage", salesGoodsRequest.getPage());
            model.addAttribute("totalPages", (salesGoodsRequest.getTotalRecords() / salesGoodsRequest.getRows()) + 1);
            model.addAttribute("keyword", salesGoodsRequest.getKeyword());
            model.addAttribute("titleName", "订单列表");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "/goodsArchives/salesGoods/index";
    }



    /**
     * 编辑售卖格商品规
     *
     * @param model 页面传参
     * @return 页面
     */
    @RequestMapping(value = "/updateSalesGoodsStatus.json")
    @ResponseBody
    public JsonResult updateSalesGoodsStatus(Model model, GoodsArchivesRequest goodsArchivesRequest) {
        try {
            try {
                if (iGoodsArchivesService.updateStatus(goodsArchivesRequest)) {
                    return new JsonResult();
                } else {
                    return new JsonResult(FAILURE.code, "操作失败！");
                }
            } catch (Exception ex) {
                return new JsonResult(FAILURE.code, "信息不合法！");
            }

        } catch (Exception e) {
            logger.error("编辑售卖格商品规异常:" + e.getMessage(), e);
        }
        return new JsonResult();
    }

    /**
     * 编辑售卖格商品规
     *
     * @param model 页面传参
     * @return 页面
     */
    @RequestMapping(value = "/updatePrice.json")
    @ResponseBody
    public JsonResult updatePrice(Model model, SpecificationGoods specificationGoods) {
        try {
            try {
                if (iSpecificationGoodsService.modifySpecificationGoodsByCustom(specificationGoods)) {
                    return new JsonResult();
                } else {
                    return new JsonResult(FAILURE.code, "操作失败！");
                }
            } catch (Exception ex) {
                return new JsonResult(FAILURE.code, "信息不合法！");
            }

        } catch (Exception e) {
            logger.error("编辑售卖格商品规异常:" + e.getMessage(), e);
        }
        return new JsonResult();
    }

}
